package com.ruteroelmeromero.utils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.faces.model.SelectItem;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

public class Conexion {
	public static Connection getConexion() {
		Connection con = null;
		try {
			InitialContext cxt = new InitialContext();
			DataSource ds = (DataSource) cxt
					.lookup("java:/comp/env/jdbc/postgres");
			con = ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	public static List<Map> listar(String query) {
		List<Map> lista = null;
		Connection con = null;
		try {
			con = getConexion();
			try {
				QueryRunner run = new QueryRunner();
				ResultSetHandler h = new MapListHandler();
				lista = run.query(con, query, h);
			} finally {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lista;
	}

	public static List<Map> listar(String query, Object... params) {
		List<Map> lista = null;
		Connection con = null;
		try {
			con = getConexion();
			try {
				QueryRunner run = new QueryRunner();
				ResultSetHandler h = new MapListHandler();
				lista = run.query(con, query, h, params);
			} finally {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lista;
	}
	
	public static List<SelectItem> combo(String query,String clave,String valor) {
		List<SelectItem> lista = null;
		List<Map> lista2 = null;
		Connection con = null;
		try {
			con = getConexion();
			try {
				QueryRunner run = new QueryRunner();
				ResultSetHandler h = new MapListHandler();
				lista2 = run.query(con, query, h);
			} finally {
				con.close();
			}
			lista = new ArrayList<SelectItem>();
			SelectItem item=null;
			for(Map dato: lista2){
				item = new SelectItem(dato.get(clave).toString(),dato.get(valor).toString());
				lista.add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lista;
	}

	public static SelectItem dato(String query,String clave,String valor) {
		SelectItem lista = null;
		List<Map> lista2 = null;
		Connection con = null;
		try {
			con = getConexion();
			try {
				QueryRunner run = new QueryRunner();
				ResultSetHandler h = new MapListHandler();
				lista2 = run.query(con, query, h);
			} finally {
				con.close();
			}
			SelectItem item=null;
			for(Map dato: lista2){
				item = new SelectItem(dato.get(clave).toString(),dato.get(valor).toString());
			}
			lista = item;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lista;
	}

	public static Map<String, String> convertir(String dato) {
		Map<String, String> ret = new HashMap<String, String>();
		dato = dato.trim().substring(1, dato.length() - 1);
		StringTokenizer tokenizer = new StringTokenizer(dato, ",");
		String par, token,clave = null;
		int cuenta = 0;
		while (tokenizer.hasMoreTokens()) {
			par = tokenizer.nextToken();
			StringTokenizer tokenizer2 = new StringTokenizer(par.trim(), "=");
			while (tokenizer2.hasMoreTokens()) {
				token = tokenizer2.nextToken();
				switch (cuenta % 2) {
				case 0:
					clave = token;
					break;
				case 1:
					ret.put(clave, token);
					break;
				}
				cuenta++;
			}
		}
		return ret;
	}
	
	public static void update(String sql, Object[][] params){
		Connection con = Conexion.getConexion();
		try{
			try{
				QueryRunner qry = new QueryRunner();
				qry.batch(con, sql, params);
			}finally{
				con.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}			
	}
	
	public static void funcion(String sql, Object[] params){
		Connection con = Conexion.getConexion();
		try{
			try{
				QueryRunner qry = new QueryRunner();
				ResultSetHandler h = new MapListHandler();
				qry.query(con, sql, params, h);
			}finally{
				con.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}			
	}
}
